package com.example.sup.conttroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sup.conttroller.request.PrisonQueryRequest;
import com.example.sup.conttroller.request.PrisonTypeAddRequest;
import com.example.sup.conttroller.request.PrisonTypeDeleteRequest;
import com.example.sup.conttroller.request.PrisonTypeEditRequest;
import com.example.sup.conttroller.response.ServerResponse;
import com.example.sup.entity.PrisonJack;
import com.example.sup.entity.PrisonType;
import com.example.sup.mapper.PrisonJackMapper;
import com.example.sup.service.PrisonJackService;
import com.example.sup.service.PrisonTypeService;

import com.example.sup.vo.PrisonConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PrisonTypeController
 * @Description {TODO}
 * @Author 李哲
 * @Date 16:31 2024/7/22
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/prisontype")
public class PrisonTypeController {

    @Resource
    private PrisonTypeService prisonTypeService;
    @Resource
    private PrisonJackService jackService;

    @PostMapping("/query")
    public ServerResponse query(@RequestBody PrisonQueryRequest request){

        //1.参数校验
        //2.调用service
        //分页查询
        IPage<PrisonType> page = new Page<>(request.getCurrent(), request.getSize());
        IPage<PrisonType> pageResult = prisonTypeService.page(page);
        List<PrisonConfigVO> list = prisonTypeService.queryPrisonTypeConfig();

        Page<PrisonConfigVO> pageInfo = new Page<>();
        pageInfo.setRecords(list);
        pageInfo.setCurrent(pageResult.getCurrent());
        pageInfo.setSize(pageResult.getSize());
        pageInfo.setTotal(pageResult.getTotal());

        //3.返回model
        return ServerResponse.successWithMsg("查询成功",pageInfo);

    }

    @PostMapping("/editPriType")
    public ServerResponse editPriType(@RequestBody PrisonTypeEditRequest request){

        //1.参数校验
        PrisonType prisonType = new PrisonType();
        prisonType.setPrisonTypeId(request.getPrisonTypeId());
        prisonType.setPrisonTypeName(request.getPrisonTypeName());

        //1.2对应prison_jack表数据

        //strong代表jack_type=1，weak代表jack_type=4,数组下标为0的元素为prison_jack_name
        String[] strong = request.getStrong();
        String[] weak = request.getWeak();


        //2.调用service
        boolean b = prisonTypeService.updateById(prisonType);
        //根据strong数组里的值遍历更新，下标为0的元素即prison_jack_number=1所对应的person_jack_name的内容，
        //                      下标为1的元素为prison_jack_number=2所对应的person_jack_name的内容.
        //                      ....以此类推（前提是prison_type_id相同）
        for (int i = 0; i < strong.length; i++) {
            QueryWrapper<PrisonJack> wrapper = new QueryWrapper<>();
            wrapper.eq("prison_jack_number", i + 1)
                    .eq("prison_type_id", request.getPrisonTypeId())
                    .eq("jack_type", 1);
            PrisonJack jack = new PrisonJack();
            jack.setPrisonJackName(strong[i]);
            jack.setJackType(1);
            jack.setPrisonJackNumber(i + 1);
            jack.setPrisonTypeId(request.getPrisonTypeId());
            boolean b1 = jackService.saveOrUpdate(jack, wrapper);
        }

        //2.2weak数组里的数据
        if (weak == null || weak.length == 0) {
            QueryWrapper<PrisonJack> wrapper = new QueryWrapper<>();
            wrapper.eq("prison_type_id", request.getPrisonTypeId())
                    .eq("jack_type", 4); // Assuming '4' is the value you want to match for 'jack_type'
            jackService.remove(wrapper);
        }
        for (int i = 0; i < weak.length; i++) {
            QueryWrapper<PrisonJack> wrapper = new QueryWrapper<>();
            wrapper.eq("prison_jack_number", i + 1)
                    .eq("prison_type_id", request.getPrisonTypeId())
                    .eq("jack_type", 4);
            PrisonJack jack = new PrisonJack();
            jack.setJackType(4);
            jack.setPrisonJackName(weak[i]);
            jack.setPrisonJackNumber(i + 1);
            jack.setPrisonTypeId(request.getPrisonTypeId());
            boolean b1 = jackService.saveOrUpdate(jack, wrapper);
        }


        //3.返回model
        if (b){
            return ServerResponse.successWithMsg("操作成功",null);
        }
        return ServerResponse.failWithMsg("操作失败");

    }

    @PostMapping("/addPriType")
    public ServerResponse addPriType(@RequestBody PrisonTypeAddRequest request) {
        //1.参数校验
        //1.1prisonType表数据
        PrisonType prisonType = new PrisonType();
        prisonType.setPrisonTypeName(request.getPrisonTypeName());
        //1.2对应prison_jack表数据

        //strong代表jack_type=1，weak代表jack_type=4,数组下标0即是通道1（对应prison_jack_number=1）下标为0的元素为prison_jack_name
        String[] strong = request.getStrong();
        String[] weak = request.getWeak();

        //2.调用service
        //2.1添加prisonType表数据
        boolean save = prisonTypeService.save(prisonType);
        //获取主键值
        Integer prisonTypeId = prisonType.getPrisonTypeId();

        //2.2添加prison_jack表数据(批量添加)
        List<PrisonJack> list = new ArrayList<>();
        for (int i = 0; i < strong.length; i++) {
            PrisonJack jack = new PrisonJack();
            jack.setPrisonJackName(strong[i]);
            jack.setJackType(1);
            jack.setPrisonTypeId(prisonTypeId);
            jack.setPrisonJackNumber(i + 1);
            list.add(jack);
        }
        boolean batch = jackService.saveBatch(list);

        // 在添加新项前清空列表
        list.clear();

        for (int i = 0; i < weak.length; i++) {
            PrisonJack jack = new PrisonJack();
            jack.setPrisonJackName(weak[i]);
            jack.setJackType(4);
            jack.setPrisonTypeId(prisonTypeId);
            jack.setPrisonJackNumber(i + 1);
            list.add(jack);
        }
        boolean batch1 = jackService.saveBatch(list);
        //3.返回model
        if (save && batch && batch1) {
            return ServerResponse.successWithMsg("操作成功", null);
        } else {
            return ServerResponse.failWithMsg("操作失败");
        }


    }


    @PostMapping("/delPriType")
    public ServerResponse deletePriType(@RequestBody PrisonTypeDeleteRequest request) {
        //1.参数校验
        //1.1prisonType表数据
        Integer prisonTypeId = request.getPrisonTypeId();

        //2.调用service
        //2.1删除prisonType表数据
        boolean b = prisonTypeService.removeById(prisonTypeId);
        //2.2删除prison_jack表数据
        QueryWrapper<PrisonJack> wrapper = new QueryWrapper<>();
        wrapper.eq("prison_type_id", prisonTypeId);
        boolean b1 = jackService.remove(wrapper);
        if (b && b1) {
            return ServerResponse.successWithMsg("操作成功", null);
        } else {
            return ServerResponse.failWithMsg("操作失败");
        }
    }

}
